# -*- coding:utf-8 -*-
"""
Author：Administrator
Date:2021年12月24日
"""
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
import numpy as np

df1 = pd.read_excel('../source/广告费 - 10.5.xlsx')
df2 = pd.read_excel('../source/销售表 - 10.5.xlsx')
df1['投放日期'] = pd.to_datetime(df1['投放日期'])
df1 = df1.set_index('投放日期', drop=True)
df2 = df2[['日期', '销售码洋']]
df2['日期'] = pd.to_datetime(df2['日期'])
df2 = df2.set_index('日期')
df_x = df1.resample('M').sum().to_period('M')
df_y = df2.resample('M').sum().to_period('M')

clf = linear_model.LinearRegression()  # 创建线性模型
# X为广告费,y为销售收入
x = pd.DataFrame(df_x['支出'])
y = pd.DataFrame(df_y['销售码洋'])

clf.fit(x, y)
k = clf.coef_
b = clf.intercept_

x0 = np.array([12000, 13000, 15000, 18000, 20000, 25000])
x0 = x0.reshape(6, 1)
y0 = clf.predict(x0)
print('预测销售收入:')
print(y0)

y_pred = clf.predict(x)
plt.rc('font',family='SimHei',size=11)
plt.figure('京东电商销售数据分析与预测')
plt.scatter(x,y,color='r')
plt.plot(x,y_pred,color='blue',linewidth=1.5)
plt.ylabel(u'销售收入(元)')
plt.xlabel(u'广告费(元)')
plt.subplots_adjust(left=0.2)
plt.show()


# print(x0)
